package genetic.q2;

import java.util.LinkedList;
import java.util.List;

import genetic.Function;
import genetic.Genotype;
import genetic.Phenotype;
import genetic.Terminal;
import genetic.TreeNode;
import genetic.q2.functions.*;

public class RobyPhenotype extends Phenotype {
	private static final int INIT_DMAX = 4;
	private static final int MUTATE_DMAX = 3;

	double average = 0;

	public RobyPhenotype() {
		super();
		List<Function> functions = new LinkedList<Function>();
		List<Terminal> terminals = new LinkedList<Terminal>();
		functions.add(new IFCAN());
		functions.add(new IFLTE());
		terminals.add(new MVN());
		terminals.add(new MVS());
		terminals.add(new MVE());
		terminals.add(new MVW());
		terminals.add(new MVR());
		terminals.add(new STP());
		terminals.add(new PU());
		init(functions, terminals);
	}

	@Override
	public Genotype[] crossover(Genotype g1, Genotype g2) {
		return crossoverDefault(g1, g2, 1);
	}

	@Override
	public double fitness(Genotype g) {
		// TODO Auto-generated method stub
		return ((RobyGenotype)g).getScore();
	}

	@Override
	public List<Genotype> getInitialPopulation(int populationSize) {
		return getInitialPopulationCommon(populationSize, INIT_DMAX);
	}

	@Override
	public Genotype makeInstanceFromTree(TreeNode tree) {
		return new RobyGenotype(tree);
	}

	@Override
	public Genotype mutate(Genotype g) {
		return super.mutateDefault(g, MUTATE_DMAX);
	}

	@Override
	public Genotype[] selectParents(List<Genotype> population,
			int populationSize) {
		return super.selectMatingPoolTournamentSelection3(population, populationSize,5);
	}

	@Override
	public void updateFitness(List<Genotype> population) {
		average = 0;
		board b = new board();
		for (int i = 0; i < population.size(); i++) {
			((RobyGenotype)population.get(i)).resetScore();
		}
		for (int i = 0; i < population.size(); i++) {
			for (int j = 0; j < 100; j++) {				
				((RobyGenotype)population.get(i)).move(b);
			}			
		}		
	}

	@Override
	public void doEvery50(List<Genotype> population) {
		// TODO Auto-generated method stub
		
	}

}
